﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using SQLEditor.admin.entity;
using SQLEditor.dal;
namespace SQLEditor.admin.service
{
    public class SqlProjectService : BaseService
    {
        public string getall()
        {
            var list = from b in DB.sqlProject                       
                       
                       select new SqlProject
                       {
                           id = b.id,
                           name = b.name,
                           remark = b.remark
                       };
            string json = JsonUtil.ToJson(list.ToList());
            return json;
        }

        public string search(int pageSize, int pageIndex, string order, string where) {
            int totalCount = 0;            
            SqlParameter paramTotalCount = new SqlParameter("@totalCount", totalCount);
            paramTotalCount.Direction = ParameterDirection.Output;
            var pager = DB.sqlProject.SqlQuery("exec sp_Pager @tableName,@columns,@order,@pageSize,@pageIndex,@where,@totalCount output",
                GetPagerParameters("sqlProject","*",order,where,pageSize,pageIndex,paramTotalCount)).ToList();
            var list = from b in pager
                       
                       select new SqlProject
                       {
                           id = b.id,
                           name = b.name,
                           remark = b.remark
                       };
            var l = list.ToList();
            totalCount = Convert.ToInt32(paramTotalCount.Value);
            string json = JsonUtil.ToJson(new { total = totalCount, rows = l });
            return json;
        }

        public string add(sqlProject b) {
            DB.sqlProject.Add(b);
            DB.SaveChanges();
            string json = JsonUtil.ToJson(new { success=true });
            return json;
        }

        public string edit(sqlProject b)
        {
            var old = DB.sqlProject.FirstOrDefault(m => m.id == b.id);
            old.name = b.name;
            old.remark = b.remark;
            
            DB.SaveChanges();
            string json = JsonUtil.ToJson(new { success = true });
            return json;
        }

        public string delete(string strIds) {
            if (string.IsNullOrEmpty(strIds)) {
                return JsonUtil.ToJson(new { success=false,msg = "没有选择要删除的记录！"});
            }
            string[] arr = strIds.Split(',');
            List<int> ids= new List<int>();
            foreach (var id in arr) {
                ids.Add(int.Parse(id));
            }
            var projects = DB.sqlProject.Where(m => ids.Contains(m.id)).ToList();
            if (projects != null)
            {
                projects.ForEach(m => DB.sqlProject.Remove(m));
                DB.SaveChanges();
                return JsonUtil.ToJson(new { success = true });
            }
            else {
                return JsonUtil.ToJson(new { success = false,msg="删除失败！" });
            }
        }
        
    }
}